import { login, logout, menuList, getInfo } from '@/api/user'
import { getToken, setToken, removeToken, setInfo, removeInfo, removeMenu, setMenu } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    // name: '',
    // avatar: '',
    user: '',
    roles: '',
    permissions: '',
    menu: [],
    leftMenu: [],
    activeName: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  // SET_NAME: (state, name) => {
  //   state.name = name
  // },
  SET_USER: (state, name) => {
    state.user = name
  },
  SET_ROLES: (state, name) => {
    state.roles = name
  },
  SET_PERMISSIONS: (state, name) => {
    state.permissions = name
  },
  // SET_AVATAR: (state, avatar) => {
  //   state.avatar = avatar
  // },
  SET_MENU: (state, menu) => {
    state.menu = menu
  },
  SET_LEFT_MENU: (state, leftMenu) => {
    state.leftMenu = leftMenu
  },
  SET_ACTIVE_NAME: (state, activeName) => {
    state.activeName = activeName
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    // console.log(userInfo, 'userInfo')
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { user, roles, permissions } = data
        // commit('SET_NAME', name)
        // commit('SET_AVATAR', avatar)
        commit('SET_USER', user)
        commit('SET_ROLES', roles)
        commit('SET_PERMISSIONS', permissions)
        setInfo(JSON.stringify(data))
        // commit('SET_MENU', menu)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
      // menuList().then(response => {
      //   const { data } = response
      //   console.log(data)
      //   if (!data) {
      //     return reject('Verification failed, please Login again.')
      //   }
      //   commit('SET_MENU', data)
      //   resolve(data)
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // get user info
  menuList({ commit, state }) {
    return new Promise((resolve, reject) => {
      menuList().then(response => {
        const { data } = response
        // console.log(data)
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        commit('SET_MENU', data)
        setMenu(JSON.stringify(data))
        console.log(state.activeName)
        if (Object.keys(state.activeName) && Object.keys(state.activeName).length > 0) {
          data.forEach(item => {
            if (item.name === state.activeName.name) {
              if (item.children) {
                router.push({ name: state.activeName.name })
                // router.push({ name: item.children[0].name })
              } else {
                router.push({ name: item.name })
              }
            }
          })
        }
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeInfo() // must remove  token  first
        removeMenu() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      removeInfo() // must remove  token  first
      removeMenu() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },
  // 设置左侧菜单
  setLeftMenu({ commit }, leftMenu) {
    commit('SET_LEFT_MENU', leftMenu)
  },
  setActiveName({ commit }, activeName) {
    commit('SET_ACTIVE_NAME', activeName)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

